Tutorial 6: Visualizations

R Bootcamp HTML Slides

Jared Knowles

Overview

In this lesson we hope to learn: - How to draw diagnostic plots in base graphics - Colors - `ggplot2’ - Basic geoms - Layering and faceting plots - Putting it together

Graphics Matter

Basic Plot

library(ggplot2)
qplot(readSS, mathSS, data = df)
plot of chunk plot1

plot of chunk plot1

Variations

qplot(readSS, mathSS, data = df) + geom_smooth()
plot of chunk plot2

plot of chunk plot2

qplot(readSS, mathSS, data = df, alpha = I(0.3))
plot of chunk plot2

plot of chunk plot2

qplot(readSS, mathSS, data = df) + xlab("Reading Score") + ylab("Math Score")
plot of chunk plot2

plot of chunk plot2

qplot(readSS, mathSS, data = df, color = race) + scale_color_brewer(type = "qual", 
    palette = 2)
plot of chunk plot2

plot of chunk plot2

Visualization Philosophy

Our data

A few pro tips

Draw a Colorwheel to show this problem

colwheel <- "https://dl.dropbox.com/u/1811289/colorwheel.R"
dropbox_source(colwheel)
col.wheel("magenta", nearby = 2)
plot of chunk colorwheel

plot of chunk colorwheel

##  [1] "plum"        "violet"      "darkmagenta" "magenta4"    "magenta3"   
##  [6] "magenta2"    "magenta"     "magenta1"    "orchid4"     "orchid"     
col.wheel("orange", nearby = 2)
plot of chunk colorwheel

plot of chunk colorwheel

##  [1] "salmon1"       "darksalmon"    "orangered4"    "orangered3"   
##  [5] "coral"         "orangered2"    "orangered"     "orangered1"   
##  [9] "lightsalmon2"  "lightsalmon"   "peru"          "tan3"         
## [13] "darkorange2"   "darkorange4"   "darkorange3"   "darkorange1"  
## [17] "linen"         "bisque3"       "bisque1"       "bisque2"      
## [21] "darkorange"    "antiquewhite3" "antiquewhite1" "papayawhip"   
## [25] "moccasin"      "orange2"       "orange"        "orange1"      
## [29] "orange4"       "wheat4"        "orange3"       "wheat"        
## [33] "oldlace"      
col.wheel("brown", nearby = 2)
plot of chunk colorwheel

plot of chunk colorwheel

##  [1] "snow1"       "snow2"       "rosybrown"   "rosybrown1"  "rosybrown2" 
##  [6] "rosybrown3"  "rosybrown4"  "lightcoral"  "indianred"   "indianred1" 
## [11] "indianred3"  "brown"       "brown4"      "brown1"      "brown3"     
## [16] "brown2"      "firebrick"   "firebrick1"  "chocolate"   "chocolate4" 
## [21] "saddlebrown" "seashell3"   "seashell2"   "seashell4"   "sandybrown" 
## [26] "peachpuff2"  "peachpuff3" 

Start with a great example

library(grid)
p1 <- qplot(readSS, ..density.., data = df, fill = race, position = "fill", 
    geom = "density") + scale_fill_brewer(type = "qual", palette = 2)

p2 <- qplot(readSS, ..fill.., data = df, fill = race, position = "fill", geom = "density") + 
    scale_fill_brewer(type = "qual", palette = 2) + ylim(c(0, 1)) + theme_bw() + 
    opts(legend.position = "none", axis.text.x = theme_blank(), axis.text.y = theme_blank(), 
        axis.ticks = theme_blank(), panel.margin = unit(0, "lines")) + ylab("") + 
    xlab("")

vp <- viewport(x = unit(0.65, "npc"), y = unit(0.73, "npc"), width = unit(0.2, 
    "npc"), height = unit(0.2, "npc"))
print(p1)
print(p2, vp = vp)
plot of chunk premier

plot of chunk premier

Now, how?

Aesthetics

ggplot(df, aes(x = readSS, y = mathSS)) + geom_point()
plot of chunk extended

plot of chunk extended